<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;


class TeacherOrder extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'teacher_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'subject_text',
        'grade_text',
        'per_week_time_text'
    ];

    protected static function init()
    {
        TeacherOrder::event('after_update', function ($row) {
            $changed = $row->getChangedData();
            if(isset($changed['status']) && $changed['status']=='-2'){
                //退课返还信息券
                $original = $row->origin;
                $user_coupons = UserCoupons::where(['id'=>$original['user_coupons_id']])->find();
                if($user_coupons){
                    $user_coupons->setInc('nums');
                    $user_coupons->save();

                    (new UserCouponsLog())->save([
                        'nums' => 1,
                        'price' => $user_coupons['price'],
                        'coupons_id' => $user_coupons['coupons_id'],
                        'user_coupons_id' => $user_coupons['id'],
                        'user_id'=>$original['user_id'],
                        'type'=>'back',
                        'order_sn'=>$original['order_sn'],
                    ]);
                }
            }
        });
    }

    

    
    public function getStatusList()
    {
        return ['-3' => __('Status -3') ,'-2' => __('Status -2'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getSubjectTextAttr($value, $data){
        $name = (new Subject)->where(['id' => $data['subject_id']])->value('name');
        return $name;
    }

    public function getGradeTextAttr($value, $data){
        $name = (new Grade)->where(['id' => $data['grade_id']])->value('name');
        return $name;
    }

    public function getPerWeekTimeTextAttr($value, $data){
        $week_cn = ['周一','周二','周三','周四','周五','周六','周日'];
        $week_en = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
        $week_key = array_search($data['per_week'], $week_en);
        return $week_cn[$week_key].' '.$data['per_week_time'];
    }

    public function teacher()
    {
        return $this->belongsTo('app\common\model\Teacher', 'teacher_id', 'user_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
